﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

public class MainGame : MonoBehaviour
{
    public static DataCenter DC = new DataCenter();
    static MainGame s_instance;
    MatterGridBehaviour[,] _grids;

    public GameObject layerbox;
    public GameObject gridInfo;
    public ItemList itemList;
//    public GameObject dialog;
//    public GameObject Tip;

    public GameObject Left;
    public GameObject RightController;
//    public GameObject GameOverObj;
//    public GameObject ChenguanTip;
    public static MainGame Instance
    {
        get
        {
            return s_instance;
        }
    }



//    public Material layerM;
    public GameObject tile;

    public Actor themainController;

    void Awake()
    {
        if(s_instance != null)
        {
            Debug.LogError("存在2个MainGame!!!");
        }
        s_instance = this;
    }

//    public void DialogOver()
//    {
//        layerbox.SetActive(true);
//        gridInfo.SetActive(true);
//        dialog.SetActive(false);
//        Tip.SetActive(true);
//        Left.SetActive(true);
//        Controller.SetActive(true);
//        MyInput.Enable = true;
//        themainController.EnableController();
 //   }

    Scene _scene = new Scene();

    public Scene Scene
    {
        get
        {
            return _scene;
        }
    }

    float startenergy = 0.0f;
    float startm = 0.0f;


    System.IO.Stream LoadFromFile(string name)
    {
#if UNITY_EDITOR1
        System.IO.Stream fs = System.IO.File.OpenRead("Assets/" + name);
        return fs;
#else
        TextAsset ta = Resources.Load<TextAsset>(name);
        return new System.IO.MemoryStream(ta.bytes);
#endif
    }

    // Use this for initialization
    void Start ()
    {

        layerbox.SetActive(true);
        gridInfo.SetActive(true);
        Left.SetActive(true);
        RightController.SetActive(true);
        System.IO.Stream fs = LoadFromFile(MainGame.DC.mapName);
        _scene = Scene.InitFrom(fs);
        _grids = new MatterGridBehaviour[_scene.SceneWidth, _scene.SceneHeight];
        for (int i = 0; i < _scene.SceneWidth; i++)
        {
            for(int j = 0; j < _scene.SceneHeight; j++)
            {
                GameObject go = GameObject.Instantiate(tile) as GameObject;
                go.name = i.ToString() + "_" + j.ToString();
                go.transform.localPosition = new Vector2(i , j);
                var grid = go.GetComponent<MatterGridBehaviour>();
                grid.SetMatterGrid(_scene.GetGrid(i, j));
                _grids[i, j] = grid;
            }
        }
        _scene.StartUpdate();
        startenergy += _scene.GetTotalEnergy();
        startm += _scene.GetTotalMass();
        themainController = GameObject.Instantiate(themainController);
        themainController.transform.localPosition = DC.startPosition + new Vector2(0.0f,0.05f);
        Controller.instance.PutItemEvent += this.OpenItemDialog;
//        themainController.DisableController();
    }
	
	// Update is called once per frame
	void Update ()
    {
        _scene.BeginUpdate();
        _scene.Update();
        _scene.EndUpdate();
    }




    void UpdateSceneView()
    {

    }


    public MatterGridBehaviour GetMatterGridByWorldPosition(Vector3 position)
    {
        int x = (int)position.x;
        int y = (int)position.y;
        if (x > 0 && x < _scene.SceneWidth && y > 0 && y < _scene.SceneHeight)
        {
            return _grids[x, y];
        }
        return null;
    }


    public void GameOver()
    {
        LeftController.instance.enabled = false;
    }

    void OpenItemDialog()
    {
        itemList.Open();
    }

}
